﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE;
using System.Xml.Linq;
using System.IO;

namespace CodeGenerator.Mvvm.ViewModelGenerator
{
    public class ProjectItemInfo
    {

        public ProjectItemInfo(Project currentProject, ProjectItem projItem, string subFolders)
        {
            this.CurrentProject = currentProject;
            this.ProjectItem = projItem;
            this.Subfolders = subFolders;
        }

        public ProjectItemInfo(Project currentProject, ProjectItem projItem)
        {
            this.CurrentProject = currentProject;
            this.ProjectItem = projItem;         
        }

        public ProjectItem ProjectItem
        {
            get;
            private set;
        }

        public string Subfolders
        {
            get;
            private set;
        }

        public Project CurrentProject
        {
            get;
            private set;
        }

        internal string GetNamespaceReference()
        {
            // Get the custom tool namespace, if any
            EnvDTE.Properties props = this.ProjectItem.Properties;
            string customToolNamespace = (string)props.Item("CustomToolNamespace").Value;
            string namespaceRef = default(string);
            string folder = string.Empty;

            if (string.IsNullOrEmpty(customToolNamespace))
            {
                if (IsCurrentProject) 
                {
                    if (string.IsNullOrEmpty(this.Subfolders))
                    {
                        // use the project reference to determine the namespace to include
                        namespaceRef = (string)this.ProjectItem.ContainingProject.Properties.Item("DefaultNamespace").Value;
                        namespaceRef = string.Concat(namespaceRef, ".", Subfolders);
                    }
                }
                else
                {
                    namespaceRef = (string)this.ProjectItem.ContainingProject.Properties.Item("DefaultNamespace").Value;
                    if (!string.IsNullOrEmpty(Subfolders))
                    {
                        namespaceRef = string.Concat(namespaceRef, ".", Subfolders);
                    }
                }
            }
            else
            {
                namespaceRef = customToolNamespace;
            }


            return namespaceRef;
        }


        private bool IsCurrentProject
        {
            get
            {
                return this.ProjectItem.ContainingProject == this.CurrentProject;
            }
        }

        public XElement GetCSDL()
        {
            XElement csdlContent = null;
            XNamespace edmxns = "http://schemas.microsoft.com/ado/2007/06/edmx";
            XNamespace edmns = "http://schemas.microsoft.com/ado/2006/04/edm";

            string fileContent = File.ReadAllText(this.ProjectItem.get_FileNames(0));

            XDocument edmxDoc = XDocument.Load(new StringReader(fileContent));
            if (edmxDoc != null)
            {
                XElement edmxNode = edmxDoc.Element(edmxns + "Edmx");
                if (edmxNode != null)
                {
                    XElement runtimeNode = edmxNode.Element(edmxns + "Runtime");
                    if (runtimeNode != null)
                    {
                        XElement conceptualModelsNode = runtimeNode.Element(edmxns + "ConceptualModels");
                        if (conceptualModelsNode != null)
                        {
                            csdlContent = conceptualModelsNode.Element(edmns + "Schema");
                        }
                    }
                }
            }
            return csdlContent;
        }

        internal void EnsureReference()
        {
            if (!IsCurrentProject)
            {
                bool isProjRefFound = false;
                VSLangProj.VSProject vsProject = (VSLangProj.VSProject)this.CurrentProject.Object;
                VSLangProj.References refs = vsProject.References;
                foreach (VSLangProj.Reference curRef in refs)
                {
                    if (curRef.SourceProject == this.ProjectItem.ContainingProject)
                    {
                        isProjRefFound = true;
                        break;
                    }
                }

                if (!isProjRefFound)
                {

                    vsProject.References.AddProject(this.ProjectItem.ContainingProject);
                }


            }
        }
    }
}
